package com.kl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.Menu;
import com.kl.entity.MenuRole;
import com.kl.mapper.MenuMapper;
import com.kl.mapper.MenuRoleMapper;
import com.kl.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MenuRoleMapper menuRoleMapper;

    @Override
    @Transactional
    public List<Map<String, Object>> findMenuJson(Long id) {
        List<Menu> menuList = menuMapper.findMenuJson(id);
        List<Map<String,Object>>mapList = getMenuTree(menuList,0);
        return mapList;
    }


    public List<Map<String,Object>> getMenuTree(List<Menu>menuList, Integer id){
        List<Map<String,Object>>mapList = new ArrayList<>();

        //遍历menuList
        for(int i =0 ;i < menuList.size();i++){
            Map<String,Object>map = null;
            Menu menu = menuList.get(i);

            //对比
            if(menu.getPid() == id){
                map = new HashMap<>();
                map.put("id",menu.getMid());
                map.put("text", menu.getText());
                map.put("url",menu.getUrl());
                map.put("nodes",getMenuTree(menuList,menu.getMid()));
            }
            if (map != null){
                mapList.add(map);
                List<Map<String,Object>>list = (List<Map<String,Object>>)map.get("nodes");
                if (list.size() <= 0){
                    map.remove("nodes");
                }


            }
        }
        return mapList;
    }

    @Override
    @Transactional
    public List<Map<String, Object>> getMenuZtree() {
        List<Menu> menuList = menuMapper.selectList(null);
        List<Map<String, Object>> result = getMenuZTree(menuList, 0);
        return result;
    }



    public List<Map<String, Object>> getMenuZTree(List<Menu> menuList, Integer parentId){
        // 定义容器
        List<Map<String, Object>> list = new ArrayList<>();

        // 遍历集合
        for (int i = 0; i < menuList.size(); i++) {
            Map<String, Object> map = null;
            Menu menu = menuList.get(i);
            // 判断是否符合当前的子节点
            if(menu.getPid().equals(parentId)){
                map = new HashMap<>();
                map.put("id", menu.getMid());
                map.put("name", menu.getText());
                map.put("children", getMenuZTree(menuList,menu.getMid()));// 2
            }

            if(map != null){
                list.add(map);
                List<Map<String, Object>> l = (List<Map<String, Object>>)map.get("children");
                if(l.size() <= 0){
                    map.remove("children");
                }
            }
        }
        return list;
    }

    @Override
    @Transactional
    public Result addMenuInfo(Menu menu) {
        int i = menuMapper.insert(menu);
        if(i <= 0){
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    @Override
    @Transactional
    public Menu selectMenuById(Integer mid) {
        Menu menu = menuMapper.selectById(mid);
        return menu;
    }

    @Override
    @Transactional
    public Result updateMenu(Menu menu) {
        int i = menuMapper.updateById(menu);
        if(i <= 0){
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    @Override
    @Transactional
    public Result deleteMenu(Integer id) {
        int i = menuMapper.deleteById(id);
        if(i <= 0){
            return Result.build(ConstantEnum.ERROR);
        }
        QueryWrapper wrapper = new QueryWrapper<MenuRole>();
        wrapper.eq("mid",id);
        menuRoleMapper.delete(wrapper);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @Override
    @Transactional
    public List<Map<String, Object>> getMenuZtreeChecked(Integer rid) {
        /*
         * 查询权限树
         * 查询属于当前rid的 mid  中间表
         * 通过对比进行数据的拼接
         */
        List<Menu> list = menuMapper.selectList(null);
        List<Integer> mids = menuRoleMapper.selectRoleMenuById(rid);
        return  getMenuZTreeToUpdate(list, 0, mids);
    }

    @Override
    public List<String> selectPerms(Long id) {
        List<String> perms = menuMapper.selectPerms(id);
        return perms;
    }

    public List<Map<String, Object>> getMenuZTreeToUpdate(List<Menu> menuList, Integer parentId, List<Integer> mids){
        // 定义容器
        List<Map<String, Object>> list = new ArrayList<>();

        // 遍历集合
        for (int i = 0; i < menuList.size(); i++) {
            Map<String, Object> map = null;
            Menu menu = menuList.get(i);
            // 判断是否符合当前的子节点
            if(menu.getPid().equals(parentId)){
                map = new HashMap<>();
                map.put("id", menu.getMid());
                map.put("name", menu.getText());
                if(mids != null && mids.size() > 0 && mids.contains(menu.getMid())){
                    map.put("checked", true);
                }
                map.put("children", getMenuZTreeToUpdate(menuList,menu.getMid(),mids));// 2
            }

            if(map != null){
                list.add(map);
                List<Map<String, Object>> l = (List<Map<String, Object>>)map.get("children");
                if(l.size() <= 0){
                    map.remove("children");
                }
            }
        }
        return list;
    }
}
